{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/nceder/qpb4e/blob/main/code/Chapter%2012/Chapter_12.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Using the filesystem"
      ],
      "metadata": {
        "id": "6Njl2UlmXJ9A"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 12.2.2 The current working directory"
      ],
      "metadata": {
        "id": "A-Coi3vRXUxG"
      }
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Di3aNYV9jF_W",
        "outputId": "b6805923-1744-49d4-a11b-6292f34498f5",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "import os\n",
        "os.getcwd()"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'/content'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 1
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JQiAo-nRjF_X",
        "outputId": "b9cadad9-f98c-4eac-bdb8-50ff13f16eda",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "os.listdir(os.curdir)"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['.config', 'sample_data']"
            ]
          },
          "metadata": {},
          "execution_count": 2
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1dDODadvjF_Y",
        "outputId": "38a96bdd-7e4e-4277-96c6-f947f7264cd4",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "os.chdir('sample_data')    #A\n",
        "os.getcwd()"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'/content/sample_data'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 3
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 12.2.3 Accessing directories with pathlib"
      ],
      "metadata": {
        "id": "tBp6X0W2XafS"
      }
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "y3pGbxLijF_Y",
        "outputId": "224f2a5d-423e-43b8-9bce-36f52a223abd",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "import pathlib\n",
        "cur_path = pathlib.Path()\n",
        "cur_path.cwd()"
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "PosixPath('/content/sample_data')"
            ]
          },
          "metadata": {},
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 12.2.4 Manipulating pathnames"
      ],
      "metadata": {
        "id": "n9yp6JdyYGeS"
      }
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZSxWNsjXjF_Z",
        "outputId": "3225331c-8f20-467b-8b0e-3f696f38901a",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "import os\n",
        "print(os.path.join('bin', 'utils', 'disktools'))"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "bin/utils/disktools\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ahtKFVyPjF_Z",
        "outputId": "4a76d113-4e5c-4fcb-f3bd-00b3ba051481",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "path1 = os.path.join('mydir', 'bin');\n",
        "path2 = os.path.join('utils', 'disktools', 'chkdisk')\n",
        "print(os.path.join(path1, path2))"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "mydir/bin/utils/disktools/chkdisk\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "v0OJBpurjF_a",
        "outputId": "20f07ef1-4bf0-46a1-d688-3302e1ea82e1",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "import os\n",
        "print(os.path.split(os.path.join('some', 'directory', 'path')))"
      ],
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "('some/directory', 'path')\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "dqRx560xjF_a",
        "outputId": "0585edaa-d8ac-4a3c-dc8e-75f310b17641",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "import os\n",
        "os.path.basename(os.path.join('some', 'directory', 'path.jpg'))"
      ],
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'path.jpg'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3ZhkAn2sjF_a",
        "outputId": "d7b085a4-f822-491a-dd20-bd4e9ffd3f98",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "os.path.dirname(os.path.join('some', 'directory', 'path.jpg'))"
      ],
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'some/directory'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JWrNwV5CjF_a",
        "outputId": "cf768567-592d-4062-a003-505cdfef4446",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "os.path.splitext(os.path.join('some', 'directory', 'path.jpg'))"
      ],
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "('some/directory/path', '.jpg')"
            ]
          },
          "metadata": {},
          "execution_count": 10
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1UhR8qBbjF_a",
        "outputId": "0cf22d37-7362-4e5f-b5e1-a65be8991da8",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "import os\n",
        "os.path.expandvars('$HOME\\\\temp')"
      ],
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'/root\\\\temp'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 12.2.5 Manipulating pathnames with pathlib"
      ],
      "metadata": {
        "id": "WRnInY86YzOj"
      }
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "aZNBQqazjF_b",
        "outputId": "0325e836-acc4-4b1f-d3f0-0f0f5800f8c6",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "from pathlib import Path\n",
        "cur_path = Path()\n",
        "print(cur_path.joinpath('bin', 'utils', 'disktools'))"
      ],
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "bin/utils/disktools\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "i5gq69N4jF_b",
        "outputId": "b34de2f6-74aa-474c-e479-965d26820f8e",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "cur_path / 'bin' / 'utils' / 'disktools'"
      ],
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "PosixPath('bin/utils/disktools')"
            ]
          },
          "metadata": {},
          "execution_count": 13
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4gwfJfg6jF_b",
        "outputId": "eef672a9-14d7-46ec-e05c-f406492098cf",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "cur_path = Path()\n",
        "print(cur_path.joinpath('bin', 'utils', 'disktools'))"
      ],
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "bin/utils/disktools\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NcCAVNFVjF_b",
        "outputId": "f6151d33-4032-4b78-f6aa-ec096dc32283",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "a_path = Path('bin/utils/disktools')\n",
        "print(a_path.parts)"
      ],
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "('bin', 'utils', 'disktools')\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "weNtyD6BjF_b",
        "outputId": "7ddc8494-6a16-4688-cc90-fdc10f42608c",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "a_path = Path('some', 'directory', 'path.jpg')\n",
        "a_path.name"
      ],
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'path.jpg'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 16
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "OgZlegy0jF_b",
        "outputId": "63243ee8-4a50-4143-8a30-662e2ec6bad6",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "print(a_path.parent)"
      ],
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "some/directory\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "drQ_PqQCjF_b",
        "outputId": "79e5098d-05e9-4086-84b8-a692a65baea9",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "a_path.suffix"
      ],
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'.jpg'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 18
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "##12.2.6 Useful constants and functions"
      ],
      "metadata": {
        "id": "Lg4BdXmrZMVq"
      }
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2VeNyXfXjF_b",
        "outputId": "dcfa596b-1bda-461c-ee67-976ad0bb7988",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "import os\n",
        "os.name"
      ],
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'posix'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Quick Check: Manipulating paths\n",
        "\n",
        "How would you use the os module's functions to take a path to a file called `test.log` and create a new file path in the same directory for a file called `test.log.old`? How would you do the same thing using the `pathlib` module?\n",
        "What path would you get if you created a pathlib `Path` object from `os.pardir`? Try it and find out."
      ],
      "metadata": {
        "id": "Vjgns_TRR8Jf"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import os.path\n",
        "old_path = os.path.abspath('test.log')\n",
        "print(old_path)\n",
        "new_path = '{}.{}'.format(old_path, \"old\")\n",
        "print(new_path)\n",
        "\n",
        "import pathlib\n",
        "path = pathlib.Path('test.log')\n",
        "abs_path = path.resolve()\n",
        "print(abs_path)\n",
        "new_path = str(abs_path) + \".old\"\n",
        "print(new_path)"
      ],
      "metadata": {
        "id": "sVGC3bJiSKXX",
        "outputId": "e1f38b30-4718-49a3-dca3-02756999f209",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "/content/sample_data/test.log\n",
            "/content/sample_data/test.log.old\n",
            "/content/sample_data/test.log\n",
            "/content/sample_data/test.log.old\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "test_path = pathlib.Path(os.pardir)\n",
        "print(test_path)\n",
        "test_path.resolve()"
      ],
      "metadata": {
        "id": "iaBXykeoSYxX",
        "outputId": "6746e40e-7d97-4dc7-f075-7b04f05665b9",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "..\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "PosixPath('/content')"
            ]
          },
          "metadata": {},
          "execution_count": 21
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 12.3 Getting information about files"
      ],
      "metadata": {
        "id": "1X9rbQ9dZbCm"
      }
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qQEXHyxHjF_c",
        "outputId": "3b9ef40b-bd71-4b59-e3f3-508d76234bcb",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "import os\n",
        "os.path.exists('/content/sample_data/')"
      ],
      "execution_count": 22,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {},
          "execution_count": 22
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Sxp3yG_IjF_c",
        "outputId": "1ca26d17-6417-4ea3-cc74-13c03cbb5ff9",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "os.path.exists('/content/sample_data/README.md')"
      ],
      "execution_count": 23,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {},
          "execution_count": 23
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "E-WZ9IL-jF_c",
        "outputId": "57070d28-4cce-4113-a231-3581664b8b58",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "os.path.exists('/content/sample_data/ljsljkflkjs')"
      ],
      "execution_count": 24,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {},
          "execution_count": 24
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AyEq5taIjF_c",
        "outputId": "00fc4437-b11e-45ac-c8ce-3649898e6598",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "os.path.isdir('/content/sample_data/')"
      ],
      "execution_count": 25,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {},
          "execution_count": 25
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "cmKctXdBjF_c",
        "outputId": "6b6eeaf0-7ff6-49c5-e841-4d5da30d7c18",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "os.path.isdir('/content/sample_data/README.md')"
      ],
      "execution_count": 26,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {},
          "execution_count": 26
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Wn0cn2YEjF_c",
        "outputId": "22ca825a-a7da-431e-fa31-26bf340d34a6",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "\n",
        "os.path.isfile('/content/sample_data/README.md')"
      ],
      "execution_count": 27,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {},
          "execution_count": 27
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 12.3.1 Getting information about files with scandir"
      ],
      "metadata": {
        "id": "y_w0SCk5aMxC"
      }
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "SOUMHfgejF_c",
        "outputId": "a6688388-639e-43f3-f727-c26ca1ef6ede",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "with os.scandir(\"..\") as my_dir:\n",
        "    for entry in my_dir:\n",
        "        print(entry.name, entry.is_file())\n"
      ],
      "execution_count": 28,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            ".config False\n",
            "sample_data False\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 12.4 More filesystem operations"
      ],
      "metadata": {
        "id": "l4bqrqkbaaIL"
      }
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Qf_nuBBmjF_d",
        "outputId": "59731b1b-2a11-4c09-9329-3cd430b271bc",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "os.chdir('/content/sample_data')\n",
        "os.listdir(os.curdir)"
      ],
      "execution_count": 29,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['README.md',\n",
              " 'anscombe.json',\n",
              " 'california_housing_train.csv',\n",
              " 'california_housing_test.csv',\n",
              " 'mnist_train_small.csv',\n",
              " 'mnist_test.csv']"
            ]
          },
          "metadata": {},
          "execution_count": 29
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "uTQGuG_gjF_d",
        "outputId": "6011a9d4-5b88-4eb8-85d2-d40663063bba",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "import glob\n",
        "glob.glob(\"*\")"
      ],
      "execution_count": 30,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['README.md',\n",
              " 'anscombe.json',\n",
              " 'california_housing_train.csv',\n",
              " 'california_housing_test.csv',\n",
              " 'mnist_train_small.csv',\n",
              " 'mnist_test.csv']"
            ]
          },
          "metadata": {},
          "execution_count": 30
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ohMOngnyjF_d",
        "outputId": "d44c35b4-369e-41a7-8590-9f20256030af",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "glob.glob(\"*json\")"
      ],
      "execution_count": 31,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['anscombe.json']"
            ]
          },
          "metadata": {},
          "execution_count": 31
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "! touch a.tmp 1.tmp 2.tmp"
      ],
      "metadata": {
        "id": "hXtpV5QEbt6I"
      },
      "execution_count": 32,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AXghqidcjF_d",
        "outputId": "31a4708a-69cb-4df9-eb48-befed72e9b34",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "glob.glob(\"?.tmp\")"
      ],
      "execution_count": 33,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['a.tmp', '2.tmp', '1.tmp']"
            ]
          },
          "metadata": {},
          "execution_count": 33
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "LZbRh01EjF_d",
        "outputId": "7a16ce8a-2e32-40f1-93a7-900d97c3aad2",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "glob.glob(\"[0-9].tmp\")"
      ],
      "execution_count": 34,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['2.tmp', '1.tmp']"
            ]
          },
          "metadata": {},
          "execution_count": 34
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "TF9NNGGBjF_d",
        "outputId": "91183faa-933d-4be2-a8ca-9a6c4a9c072d",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "os.rename('README.md', 'README.md.old')\n",
        "os.listdir(os.curdir)"
      ],
      "execution_count": 35,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['anscombe.json',\n",
              " 'a.tmp',\n",
              " 'README.md.old',\n",
              " '2.tmp',\n",
              " '1.tmp',\n",
              " 'california_housing_train.csv',\n",
              " 'california_housing_test.csv',\n",
              " 'mnist_train_small.csv',\n",
              " 'mnist_test.csv']"
            ]
          },
          "metadata": {},
          "execution_count": 35
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1QkjFVQUjF_e",
        "outputId": "565ef0df-2ad6-4f41-db65-ab3f423c977d",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "os.remove('a.tmp')\n",
        "os.listdir(os.curdir)"
      ],
      "execution_count": 36,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['anscombe.json',\n",
              " 'README.md.old',\n",
              " '2.tmp',\n",
              " '1.tmp',\n",
              " 'california_housing_train.csv',\n",
              " 'california_housing_test.csv',\n",
              " 'mnist_train_small.csv',\n",
              " 'mnist_test.csv']"
            ]
          },
          "metadata": {},
          "execution_count": 36
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "YZZ72kJAjF_e",
        "outputId": "97e4687f-2d43-4db0-9530-a6a07bbdf9ae",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "os.makedirs('mydir')\n",
        "os.listdir(os.curdir)"
      ],
      "execution_count": 37,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['anscombe.json',\n",
              " 'mydir',\n",
              " 'README.md.old',\n",
              " '2.tmp',\n",
              " '1.tmp',\n",
              " 'california_housing_train.csv',\n",
              " 'california_housing_test.csv',\n",
              " 'mnist_train_small.csv',\n",
              " 'mnist_test.csv']"
            ]
          },
          "metadata": {},
          "execution_count": 37
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Lk_36lnEjF_e",
        "outputId": "6265bbe2-cffb-4c76-aef8-8b8069d2e73d",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "os.path.isdir('mydir')"
      ],
      "execution_count": 38,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {},
          "execution_count": 38
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "TtG2IVb-jF_e",
        "outputId": "233a8b28-9f76-4c1f-ff29-f1f0946ba72b",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "os.rmdir('mydir')\n",
        "os.listdir(os.curdir)"
      ],
      "execution_count": 39,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['anscombe.json',\n",
              " 'README.md.old',\n",
              " '2.tmp',\n",
              " '1.tmp',\n",
              " 'california_housing_train.csv',\n",
              " 'california_housing_test.csv',\n",
              " 'mnist_train_small.csv',\n",
              " 'mnist_test.csv']"
            ]
          },
          "metadata": {},
          "execution_count": 39
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 12.4.1 More filesystem operations with pathlib\n"
      ],
      "metadata": {
        "id": "0OvJ8AuUetd4"
      }
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "R6mVryZujF_e",
        "outputId": "e3422968-e85e-4b5d-89ec-426235b2d3e2",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "new_path = cur_path.joinpath('/content', 'sample_data')\n",
        "list(new_path.iterdir())"
      ],
      "execution_count": 40,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[PosixPath('/content/sample_data/anscombe.json'),\n",
              " PosixPath('/content/sample_data/README.md.old'),\n",
              " PosixPath('/content/sample_data/2.tmp'),\n",
              " PosixPath('/content/sample_data/1.tmp'),\n",
              " PosixPath('/content/sample_data/california_housing_train.csv'),\n",
              " PosixPath('/content/sample_data/california_housing_test.csv'),\n",
              " PosixPath('/content/sample_data/mnist_train_small.csv'),\n",
              " PosixPath('/content/sample_data/mnist_test.csv')]"
            ]
          },
          "metadata": {},
          "execution_count": 40
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "MWv8RyHzjF_e",
        "outputId": "35fddea7-f5df-4c50-808b-81af36d52f73",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "list(cur_path.glob(\"*\"))"
      ],
      "execution_count": 41,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[PosixPath('anscombe.json'),\n",
              " PosixPath('README.md.old'),\n",
              " PosixPath('2.tmp'),\n",
              " PosixPath('1.tmp'),\n",
              " PosixPath('california_housing_train.csv'),\n",
              " PosixPath('california_housing_test.csv'),\n",
              " PosixPath('mnist_train_small.csv'),\n",
              " PosixPath('mnist_test.csv')]"
            ]
          },
          "metadata": {},
          "execution_count": 41
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "MvgVRj8VjF_f",
        "outputId": "0195fc9a-1234-4081-938b-596494d87c1f",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "list(cur_path.glob(\"*json\"))"
      ],
      "execution_count": 42,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[PosixPath('anscombe.json')]"
            ]
          },
          "metadata": {},
          "execution_count": 42
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jbfXfCi3jF_f",
        "outputId": "acc6b89e-e5e2-4192-839e-224dcf81e14c",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "list(cur_path.glob(\"?.tmp\"))"
      ],
      "execution_count": 43,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[PosixPath('2.tmp'), PosixPath('1.tmp')]"
            ]
          },
          "metadata": {},
          "execution_count": 43
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "YB97NCu7jF_f",
        "outputId": "d03fdc86-14bc-4655-8c94-b8224beb91ff",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "list(cur_path.glob(\"[0-9].tmp\"))"
      ],
      "execution_count": 44,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[PosixPath('2.tmp'), PosixPath('1.tmp')]"
            ]
          },
          "metadata": {},
          "execution_count": 44
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "MowaxuXyjF_f",
        "outputId": "0b7cdfbd-54c4-40a7-af06-5295ed4092df",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "old_path = Path('README.md.old')\n",
        "new_path = Path('README.md')\n",
        "old_path.rename(new_path)\n",
        "list(cur_path.iterdir())"
      ],
      "execution_count": 45,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[PosixPath('README.md'),\n",
              " PosixPath('anscombe.json'),\n",
              " PosixPath('2.tmp'),\n",
              " PosixPath('1.tmp'),\n",
              " PosixPath('california_housing_train.csv'),\n",
              " PosixPath('california_housing_test.csv'),\n",
              " PosixPath('mnist_train_small.csv'),\n",
              " PosixPath('mnist_test.csv')]"
            ]
          },
          "metadata": {},
          "execution_count": 45
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "T2WsyjCKjF_f",
        "outputId": "89074c94-3bfe-4a80-fc39-0aa8820c18a5",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "new_path = Path('1.tmp')\n",
        "new_path.unlink()\n",
        "list(cur_path.iterdir())"
      ],
      "execution_count": 46,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[PosixPath('README.md'),\n",
              " PosixPath('anscombe.json'),\n",
              " PosixPath('2.tmp'),\n",
              " PosixPath('california_housing_train.csv'),\n",
              " PosixPath('california_housing_test.csv'),\n",
              " PosixPath('mnist_train_small.csv'),\n",
              " PosixPath('mnist_test.csv')]"
            ]
          },
          "metadata": {},
          "execution_count": 46
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gBo9RxZvjF_f",
        "outputId": "75eaaeed-9e06-4444-fe67-1992430ff6e0",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "new_path = Path ('mydir')\n",
        "new_path.mkdir(parents=True)\n",
        "list(cur_path.iterdir())"
      ],
      "execution_count": 47,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[PosixPath('README.md'),\n",
              " PosixPath('anscombe.json'),\n",
              " PosixPath('mydir'),\n",
              " PosixPath('2.tmp'),\n",
              " PosixPath('california_housing_train.csv'),\n",
              " PosixPath('california_housing_test.csv'),\n",
              " PosixPath('mnist_train_small.csv'),\n",
              " PosixPath('mnist_test.csv')]"
            ]
          },
          "metadata": {},
          "execution_count": 47
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "p7eLMx8BjF_f",
        "outputId": "c38e128e-1792-4370-f0bd-e326dcc75632",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "new_path.is_dir()"
      ],
      "execution_count": 48,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {},
          "execution_count": 48
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "G03O87s1jF_g",
        "outputId": "1a811c56-81ba-434f-8d87-a575a5c0c004",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "new_path = Path('mydir')\n",
        "new_path.rmdir()\n",
        "list(cur_path.iterdir())"
      ],
      "execution_count": 49,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[PosixPath('README.md'),\n",
              " PosixPath('anscombe.json'),\n",
              " PosixPath('2.tmp'),\n",
              " PosixPath('california_housing_train.csv'),\n",
              " PosixPath('california_housing_test.csv'),\n",
              " PosixPath('mnist_train_small.csv'),\n",
              " PosixPath('mnist_test.csv')]"
            ]
          },
          "metadata": {},
          "execution_count": 49
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 12.6 Lab 12: More file operations\n",
        "\n",
        "---\n",
        "\n"
      ],
      "metadata": {
        "id": "Yc9J7eNoUiGo"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "How might you calculate the total size of all files ending with .test that aren't symlinks in a directory? If your first answer was using `os` and `os.path`, also try it with `pathlib`, and vice versa.\n",
        "\n",
        "Write some code that builds off your solution, and moves the .test files detected above to a new subdirectory in the same directory called 'backup'."
      ],
      "metadata": {
        "id": "Ho5KcJfJkNoc"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 12.6.2 Solutions\n",
        "### Human (author) solutions"
      ],
      "metadata": {
        "id": "YsS_DIoRmySI"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "! cp california_housing_test.csv california_housing_test.csv.test\n",
        "! cp california_housing_train.csv california_housing_train.csv.test\n",
        "! mkdir subdir\n",
        "! cp mnist_test.csv subdir/mnist_test.csv.test\n",
        "! mkdir subdir/subdir2\n",
        "! cp mnist_train_small.csv subdir/subdir2/mnist_train_small.csv.test"
      ],
      "metadata": {
        "id": "CHYE1B6kpOTm"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Human (author) solution - pathlib library\n",
        "\n",
        "import pathlib\n",
        "cur_path = pathlib.Path(\".\")\n",
        "\n",
        "size = 0\n",
        "for text_path in cur_path.rglob(\"*.test\"):\n",
        "    if not text_path.is_symlink():\n",
        "        size += text_path.stat().st_size\n",
        "\n",
        "print(size)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "nLNJ97p6kiD6",
        "outputId": "10378ae0-250c-4eb6-a27d-8eaa08d14843"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "56820894\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Move files to backup directory"
      ],
      "metadata": {
        "id": "-u7mAsKHk48c"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Human (author) solution - pathlib library\n",
        "\n",
        "import pathlib\n",
        "cur_path = pathlib.Path(\".\")\n",
        "new_path = pathlib.Path(\"backup\")\n",
        "new_path.mkdir(exist_ok=True)   #A\n",
        "\n",
        "size = 0\n",
        "for text_path in cur_path.rglob(\"*.test\"):\n",
        "    if not text_path.is_symlink():\n",
        "        size += text_path.stat().st_size\n",
        "        text_path.rename(new_path / text_path.name)   #B\n",
        "\n",
        "print(size)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "jbsUCYfdkuTq",
        "outputId": "1f8c06ac-7f14-4757-9e37-d6724d2c087f"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "58828465\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Refactor to use `os` library"
      ],
      "metadata": {
        "id": "OSBADD1vlYpV"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "! cp sample_data/california_housing_test.csv california_housing_test.csv.test\n",
        "! cp california_housing_train.csv california_housing_train.csv.test\n",
        "! cp mnist_test.csv subdir/mnist_test.csv.test\n",
        "! cp mnist_train_small.csv subdir/subdir2/mnist_train_small.csv.test\n",
        "! rm backup/*.test"
      ],
      "metadata": {
        "id": "3W2E4KGVtlg2",
        "outputId": "58f3cef8-6253-430f-c456-5d7cf83847f0",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "cp: cannot stat 'sample_data/california_housing_test.csv': No such file or directory\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Human (author) solution - os library\n",
        "\n",
        "import os\n",
        "cur_path = \".\"\n",
        "\n",
        "size = 0\n",
        "for root, dirs, files in os.walk(os.curdir):   #A\n",
        "    for file in files:      #B\n",
        "        test_path = os.path.join(root, file)\n",
        "\n",
        "        if (not os.path.islink(test_path) and\n",
        "            os.path.splitext(test_path)[-1] == '.test'):  #C\n",
        "            size += os.path.getsize(test_path)   #D\n",
        "print(f\"{size}\")\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "OkJQgSoMlu1q",
        "outputId": "f12d5ade-8b26-4dab-a72b-a8f44ec14285"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "56519753\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Human (author) solution - os library\n",
        "\n",
        "import os\n",
        "\n",
        "cur_path = \".\"\n",
        "new_path = os.path.join(cur_path,\"backup\")\n",
        "\n",
        "# create backup directory\n",
        "os.makedirs(new_path, exist_ok=True)   #A\n",
        "size = 0\n",
        "for root, dirs, files in os.walk(cur_path,\"sample_data\"):\n",
        "    for file in files:\n",
        "        test_path = os.path.join(root, file)\n",
        "\n",
        "        if (not os.path.islink(test_path)\n",
        "          and os.path.splitext(test_path)[-1] == \".test\"):\n",
        "            size += os.path.getsize(test_path)\n",
        "            os.rename(test_path, os.path.join(new_path,\n",
        "                       os.path.basename(test_path)))   #B\n",
        "print(f\"{size}\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "uEc_ocGil4e5",
        "outputId": "5a057420-fcfb-4442-800f-5ce62bc9613e"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "58828465\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Copilot solutions"
      ],
      "metadata": {
        "id": "1GOzWudslJiI"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "! cp california_housing_test.csv california_housing_test.csv.test\n",
        "! cp california_housing_train.csv california_housing_train.csv.test\n",
        "! cp mnist_test.csv subdir/mnist_test.csv.test\n",
        "! cp mnist_train_small.csv subdir/subdir2/mnist_train_small.csv.test\n",
        "! rm backup/*.test"
      ],
      "metadata": {
        "id": "MmZBaBIfuqug"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Generated by Copilot - os library\n",
        "# txt_file_size.py\n",
        "\n",
        "import os\n",
        "\n",
        "size = 0\n",
        "for root, dirs, files in os.walk(os.curdir):\n",
        "    for file in files:\n",
        "        file_path = os.path.join(root, file)\n",
        "        if not os.path.islink(file_path) and os.path.splitext(file)[1] == '.test':\n",
        "            size += os.path.getsize(file_path)\n",
        "\n",
        "print(f\"Total size of all .test files that are not symlinks: {size} bytes\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "mPxRheLel-xB",
        "outputId": "7c33843f-35d1-46d2-9a89-61b9d67c9910"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Total size of all .test files that are not symlinks: 56820894 bytes\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Move to backup directory"
      ],
      "metadata": {
        "id": "8HH7Xlf0nByx"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Generated by Copilot - os library\n",
        "# txt_file_size_and_move.py\n",
        "\n",
        "import os\n",
        "\n",
        "import shutil\n",
        "\n",
        "size = 0\n",
        "os.makedirs('backup', exist_ok=True)\n",
        "for root, dirs, files in os.walk(os.curdir):\n",
        "    for file in files:\n",
        "        file_path = os.path.join(root, file)\n",
        "        if (not os.path.islink(file_path)\n",
        "                and os.path.splitext(file)[1] == '.test'\n",
        "                and \"backup\" not in file_path):\n",
        "            size += os.path.getsize(file_path)\n",
        "            shutil.move(file_path, 'backup')\n",
        "\n",
        "print(f\"Total size of all .test files that are not symlinks: {size} bytes\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "vLTEtn1VmHTJ",
        "outputId": "baee4169-dbe6-49d2-fa1d-f04adf5ad143"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Total size of all .test files that are not symlinks: 56820894 bytes\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Refactor with pathlib libarary"
      ],
      "metadata": {
        "id": "ZVlqyYRXnFDl"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "! cp california_housing_test.csv california_housing_test.csv.test\n",
        "! cp california_housing_train.csv california_housing_train.csv.test\n",
        "! cp mnist_test.csv subdir/mnist_test.csv.test\n",
        "! cp mnist_train_small.csv subdir/subdir2/mnist_train_small.csv.test\n",
        "! rm backup/*.test"
      ],
      "metadata": {
        "id": "w3e9AuJYus9w"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Generated by Copilot - pathlib library\n",
        "# txt_file_size_and_move.py\n",
        "\n",
        "from pathlib import Path\n",
        "\n",
        "size = 0\n",
        "backup_dir = Path('backup')\n",
        "backup_dir.mkdir(exist_ok=True)\n",
        "\n",
        "for file in Path().rglob('*'):\n",
        "    if not file.is_symlink() and file.suffix == '.test':\n",
        "        size += file.stat().st_size\n",
        "        file.rename(backup_dir / file.name)\n",
        "\n",
        "print(f\"Total size of all .test files that are not symlinks: {size} bytes\")\n"
      ],
      "metadata": {
        "id": "83omDD43mj_B",
        "outputId": "1577de8d-a190-4650-9dbe-f27e63da7475",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Total size of all .test files that are not symlinks: 58828465 bytes\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Colaboratory solutions"
      ],
      "metadata": {
        "id": "xWl02GJynJcX"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "! cp california_housing_test.csv california_housing_test.csv.test\n",
        "! cp california_housing_train.csv california_housing_train.csv.test\n",
        "! cp mnist_test.csv subdir/mnist_test.csv.test\n",
        "! cp mnist_train_small.csv subdir/subdir2/mnist_train_small.csv.test\n",
        "! rm backup/*.test"
      ],
      "metadata": {
        "id": "BN9UsHX3uuWn"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Generated by Colaboratory - os libary\n",
        "# prompt: Write a Python script to calculate the total size of all files with a\n",
        "#         .test extension that are not symlinks in the current directory and subdirectories.\n",
        "\n",
        "import os\n",
        "\n",
        "total_size = 0\n",
        "\n",
        "for root, _, files in os.walk('.'):\n",
        "    for file in files:\n",
        "        if file.endswith('.test') and not os.path.islink(os.path.join(root, file)):\n",
        "            total_size += os.path.getsize(os.path.join(root, file))\n",
        "\n",
        "print(f\"Total size of .test files: {total_size} bytes\")\n"
      ],
      "metadata": {
        "id": "8f9i_sVsKcUW",
        "outputId": "6b904017-3770-4f02-ce7a-8c15090ab9c6",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Total size of .test files: 56820894 bytes\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Move to backup directory"
      ],
      "metadata": {
        "id": "nvF1YaxanM0U"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Generated by Colaboratory - os libary\n",
        "# prompt: Modify that script in the previous cell to also move the files with a\n",
        "#         .test extension to a subdirectory of the current directory called backup\n",
        "\n",
        "import os\n",
        "\n",
        "total_size = 0\n",
        "backup_dir = os.path.join(os.getcwd(), 'backup')\n",
        "\n",
        "# Create the backup directory if it doesn't exist\n",
        "if not os.path.exists(backup_dir):\n",
        "    os.makedirs(backup_dir)\n",
        "\n",
        "for root, _, files in os.walk('.'):\n",
        "    for file in files:\n",
        "        if file.endswith('.test') and not os.path.islink(os.path.join(root, file)):\n",
        "            total_size += os.path.getsize(os.path.join(root, file))\n",
        "            # Move the file to the backup directory\n",
        "            os.rename(os.path.join(root, file), os.path.join(backup_dir, file))\n",
        "\n",
        "print(f\"Total size of .test files: {total_size} bytes\")\n"
      ],
      "metadata": {
        "id": "wFVPz8L_LQKL",
        "outputId": "77323a01-2a48-400b-cc71-95f34e68ef77",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Total size of .test files: 58828465 bytes\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Refactor with pathlib libarary"
      ],
      "metadata": {
        "id": "7HyPi5QXnUBz"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "! cp california_housing_test.csv california_housing_test.csv.test\n",
        "! cp california_housing_train.csv california_housing_train.csv.test\n",
        "! cp mnist_test.csv subdir/mnist_test.csv.test\n",
        "! cp mnist_train_small.csv subdir/subdir2/mnist_train_small.csv.test\n",
        "! rm backup/*.test"
      ],
      "metadata": {
        "id": "K5OGxBkTuwBE"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# this checks to see if the version of Python can run Path.walk()\n",
        "import sys\n",
        "from pathlib import Path\n",
        "\n",
        "if sys.version_info.minor < 12:\n",
        "  raise RuntimeError(f\"Python version must be >= 3.12 to run Path.walk(), current version is {sys.version}\")\n",
        "\n",
        "# Generated by Colaboratory - pathlib libary\n",
        "# prompt: Rewrite the script in the previous cell to use pathlib instead of os\n",
        "#         and os.path. Do not use os or os.path modules\n",
        "\n",
        "total_size = 0\n",
        "backup_dir = Path.cwd().joinpath('backup')\n",
        "\n",
        "# Create the backup directory if it doesn't exist\n",
        "if not backup_dir.exists():\n",
        "    backup_dir.mkdir(parents=True)\n",
        "\n",
        "for root, _, files in Path('.').walk():\n",
        "    for file in files:\n",
        "        if file.endswith('.test') and not file.is_symlink():\n",
        "            total_size += file.stat().st_size\n",
        "            # Move the file to the backup directory\n",
        "            file.rename(backup_dir.joinpath(file))\n",
        "\n",
        "print(f\"Total size of .test files: {total_size} bytes\")"
      ],
      "metadata": {
        "id": "3L9uWKwxMT1Z",
        "outputId": "e42a98e4-a4ba-48b0-d75a-2f71e3c0dd6e",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 211
        }
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "error",
          "ename": "RuntimeError",
          "evalue": "Python version must be >= 3.12 to run Path.walk(), current version is 3.10.12 (main, Mar 22 2024, 16:50:05) [GCC 11.4.0]",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-70-55d92d7d6b30>\u001b[0m in \u001b[0;36m<cell line: 5>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mversion_info\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mminor\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m12\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m   \u001b[0;32mraise\u001b[0m \u001b[0mRuntimeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"Python version must be >= 3.12 to run Path.walk(), current version is {sys.version}\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      8\u001b[0m \u001b[0;31m# Generated by Colaboratory - pathlib libary\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;31mRuntimeError\u001b[0m: Python version must be >= 3.12 to run Path.walk(), current version is 3.10.12 (main, Mar 22 2024, 16:50:05) [GCC 11.4.0]"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "UiCoeHTbzEaJ"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}